﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using Business;
using System.IO;
using System.Drawing;
using System.Collections;

namespace BVVD_Website.ql_hoc_vien
{
    public partial class frmHocvien : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                grvHocvien.DataSource = getData();
                grvHocvien.DataBind();


            }
        }
        protected void grvHocvien_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {

            grvHocvien.DataSource = getData();
        }

        protected void grvHocvien_ItemDataBound(object sender, GridItemEventArgs e)
        {
            BVVDDataStore<LST_DANHMUC> type = new BVVDDataStore<LST_DANHMUC>();
            if (e.Item is GridDataItem)
            {
                GridDataItem dataItem = e.Item as GridDataItem;
                if (dataItem["TRANGTHAI"].Text == "0")
                    dataItem.ForeColor = Color.WhiteSmoke;
            }
            if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
            {
                GridEditFormItem editFormItem = e.Item as GridEditFormItem;
                GridDataItem parentItem = editFormItem.ParentItem;

                RadDropDownList ddlQUOCTICH = editFormItem.FindControl("ddlQUOCTICH") as RadDropDownList;
                Hashtable quoctich = new Hashtable();
                quoctich.Add("MALOAIDM", "QUOCGIA");
                quoctich.Add("TRANGTHAI", "1");
                ddlQUOCTICH.DataSource = type.getFromDB(quoctich);
                ddlQUOCTICH.DataTextField = "TENDM";
                ddlQUOCTICH.DataValueField = "MADM";


                RadDropDownList ddlDANTOC = editFormItem.FindControl("ddlDANTOC") as RadDropDownList;
                Hashtable dantoc = new Hashtable();
                dantoc.Add("MALOAIDM", "DANTOC");
                dantoc.Add("TRANGTHAI", "1");
                ddlDANTOC.DataSource = type.getFromDB(dantoc);
                ddlDANTOC.DataTextField = "TENDM";
                ddlDANTOC.DataValueField = "MADM";

                RadDropDownList ddlTONGIAO = editFormItem.FindControl("ddlTONGIAO") as RadDropDownList;
                Hashtable tongiao = new Hashtable();
                tongiao.Add("MALOAIDM", "TONGIAO");
                tongiao.Add("TRANGTHAI", "1");
                ddlTONGIAO.DataSource = type.getFromDB(tongiao).OrderBy(a => a.TENDM);
                ddlTONGIAO.DataTextField = "TENDM";
                ddlTONGIAO.DataValueField = "MADM";


                RadDropDownList ddlCHUCVU = editFormItem.FindControl("ddlCHUCVU") as RadDropDownList;
                Hashtable chucvu = new Hashtable();
                chucvu.Add("MALOAIDM", "CHUCVU");
                chucvu.Add("TRANGTHAI", "1");
                ddlCHUCVU.DataSource = type.getFromDB(chucvu);
                ddlCHUCVU.DataTextField = "TENDM";
                ddlCHUCVU.DataValueField = "MADM";

                RadDropDownList ddlCHUCDANH = editFormItem.FindControl("ddlCHUCDANH") as RadDropDownList;
                Hashtable chucdanh = new Hashtable();
                chucdanh.Add("MALOAIDM", "CHUCDANH");
                chucdanh.Add("TRANGTHAI", "1");
                ddlCHUCDANH.DataSource = type.getFromDB(chucdanh);
                ddlCHUCDANH.DataTextField = "TENDM";
                ddlCHUCDANH.DataValueField = "MADM";

                RadDropDownList ddlTRINHDO = editFormItem.FindControl("ddlTRINHDO") as RadDropDownList;
                Hashtable trinhdo = new Hashtable();
                trinhdo.Add("MALOAIDM", "TRINHDO");
                trinhdo.Add("TRANGTHAI", "1");
                ddlTRINHDO.DataSource = type.getFromDB(trinhdo);
                ddlTRINHDO.DataTextField = "TENDM";
                ddlTRINHDO.DataValueField = "MADM";

                RadDropDownList ddlCHUYENMON = editFormItem.FindControl("ddlCHUYENMON") as RadDropDownList;
                Hashtable chuyenmon = new Hashtable();
                chuyenmon.Add("MALOAIDM", "CHUYENMON");
                chuyenmon.Add("TRANGTHAI", "1");
                ddlCHUYENMON.DataSource = type.getFromDB(chuyenmon);
                ddlCHUYENMON.DataTextField = "TENDM";
                ddlCHUYENMON.DataValueField = "MADM";

                RadDropDownList ddlGIOITINH = editFormItem.FindControl("ddlGIOITINH") as RadDropDownList;
                Hashtable gioitinh = new Hashtable();
                gioitinh.Add("MALOAIDM", "GIOITINH");
                gioitinh.Add("TRANGTHAI", "1");
                ddlGIOITINH.DataSource = type.getFromDB(gioitinh);
                ddlGIOITINH.DataTextField = "TENDM";
                ddlGIOITINH.DataValueField = "MADM";

                RadioButtonList rblDANGVIEN = editFormItem.FindControl("rblDANGVIEN") as RadioButtonList;
                RadioButtonList rblLOAIGIAY = editFormItem.FindControl("rblLOAIGIAY") as RadioButtonList;
                RadioButtonList rblDOANVIEN = editFormItem.FindControl("rblDOANVIEN") as RadioButtonList;
                RadioButtonList rblTHUONGBINH = editFormItem.FindControl("rblTHUONGBINH") as RadioButtonList;
                RadioButtonList rblLIETSY = editFormItem.FindControl("rblLIETSY") as RadioButtonList;
                RadioButtonList rblTRANGTHAI = editFormItem.FindControl("rblTRANGTHAI") as RadioButtonList;
                RadBinaryImage rbiPICTURE = editFormItem.FindControl("rbiPICTURE") as RadBinaryImage;

                RadTextBox txtMAHV = editFormItem.FindControl("txtMAHV") as RadTextBox;




                if (e.Item is GridEditFormInsertItem || e.Item is GridDataInsertItem)
                {

                }
                else
                {
                    ddlQUOCTICH.SelectedValue = editFormItem["QUOCGIA"].Text;
                    ddlDANTOC.SelectedValue = editFormItem["DANTOC"].Text;
                    ddlTONGIAO.SelectedValue = editFormItem["TONGIAO"].Text;
                    ddlCHUCVU.SelectedValue = editFormItem["CHUCVU"].Text;
                    ddlCHUCDANH.SelectedValue = editFormItem["CHUCDANH"].Text;
                    ddlTRINHDO.SelectedValue = editFormItem["TRINHDO"].Text;
                    ddlCHUYENMON.SelectedValue = editFormItem["CHUYENMON"].Text;
                    ddlGIOITINH.SelectedValue = editFormItem["GIOITINH"].Text;
                    rblDANGVIEN.SelectedValue = editFormItem["DANGVIEN"].Text;
                    rblDOANVIEN.SelectedValue = editFormItem["DOANVIEN"].Text;
                    rblLOAIGIAY.SelectedValue = editFormItem["LOAIGIAY"].Text;
                    rblTHUONGBINH.SelectedValue = editFormItem["THUONGBINH"].Text;
                    rblLIETSY.SelectedValue = editFormItem["CONLIETSY"].Text;
                    rblTRANGTHAI.SelectedValue = editFormItem["TRANGTHAI"].Text;
                    txtMAHV.ReadOnly = true;
                    txtMAHV.BackColor = Color.WhiteSmoke;

                }
                ddlQUOCTICH.DataBind();
                ddlDANTOC.DataBind();
                ddlTONGIAO.DataBind();
                ddlCHUCVU.DataBind();
                ddlCHUCDANH.DataBind();
                ddlTRINHDO.DataBind();
                ddlCHUYENMON.DataBind();
                ddlGIOITINH.DataBind();


            }

        }

        public List<LST_HOCVIEN> getData()
        {
            BVVDDataStore<LST_HOCVIEN> ds = new BVVDDataStore<LST_HOCVIEN>();
            return ds.getFromDB(new Hashtable());

        }
        protected void grvHocvien_InsertCommand(object sender, GridCommandEventArgs e)
        {

            GridEditFormInsertItem insertedItem = (GridEditFormInsertItem)e.Item;
            RadAsyncUpload rauUpload = insertedItem.FindControl("rauUpload") as RadAsyncUpload;

            LST_HOCVIEN hocvien = new LST_HOCVIEN
            {
                MAHV = (insertedItem.FindControl("txtMAHV") as RadTextBox).Text,
                HODEM = (insertedItem.FindControl("txtHODEM") as RadTextBox).Text,
                TENHV = (insertedItem.FindControl("txtTENHV") as RadTextBox).Text,
                NGAYSINH = (insertedItem.FindControl("rdpNGAYSINH") as RadDatePicker).SelectedDate,
                GIOITINH = (insertedItem.FindControl("ddlGIOITINH") as RadDropDownList).SelectedValue,
                LOAIGIAY = Int32.Parse((insertedItem.FindControl("rblLOAIGIAY") as RadioButtonList).SelectedValue),
                SOCMND = (insertedItem.FindControl("rntxtCMND") as RadTextBox).Text,
                NGAYCAP = (insertedItem.FindControl("rdpNGAYCAP") as RadDatePicker).SelectedDate,
                NOICAP = (insertedItem.FindControl("txtNOICAP") as RadTextBox).Text,
                DIACHI = (insertedItem.FindControl("txtDIACHI") as RadTextBox).Text,
                HOKHAU = (insertedItem.FindControl("txtHOKHAU") as RadTextBox).Text,
                QUEQUAN = (insertedItem.FindControl("txtQUEQUAN") as RadTextBox).Text,
                TEL = (insertedItem.FindControl("txtTEL") as RadTextBox).Text,
                MOBILE = (insertedItem.FindControl("txtMOBILE") as RadTextBox).Text,
                EMAIL = (insertedItem.FindControl("txtEMAIL") as RadTextBox).Text,
                QUOCTICH = (insertedItem.FindControl("ddlQUOCTICH") as RadDropDownList).SelectedValue,
                DANTOC = (insertedItem.FindControl("ddlDANTOC") as RadDropDownList).SelectedValue,
                TONGIAO = (insertedItem.FindControl("ddlTONGIAO") as RadDropDownList).SelectedValue,
                COQUANCT = (insertedItem.FindControl("txtCOQUAN") as RadTextBox).Text,
                CHUCVU = (insertedItem.FindControl("ddlCHUCVU") as RadDropDownList).SelectedValue,
                CHUCDANH = (insertedItem.FindControl("ddlCHUCDANH") as RadDropDownList).SelectedValue,
                TRINHDO = (insertedItem.FindControl("ddlTRINHDO") as RadDropDownList).SelectedValue,
                CHUYENMON = (insertedItem.FindControl("ddlCHUYENMON") as RadDropDownList).SelectedValue,
                DANGVIEN = Int32.Parse((insertedItem.FindControl("rblDANGVIEN") as RadioButtonList).SelectedValue),
                NGAYVAODANG = (insertedItem.FindControl("rdpNGAYVAODANG") as RadDatePicker).SelectedDate,
                DOANVIEN = Int32.Parse((insertedItem.FindControl("rblDOANVIEN") as RadioButtonList).SelectedValue),
                NGAYDOANVIEN = (insertedItem.FindControl("rdpNGAYDOANVIEN") as RadDatePicker).SelectedDate.Value,
                THUONGBINH = Int32.Parse((insertedItem.FindControl("rblTHUONGBINH") as RadioButtonList).SelectedValue),
                CONLIETSY = Int32.Parse((insertedItem.FindControl("rblLIETSY") as RadioButtonList).SelectedValue),
                THAMNIEN = Convert.ToInt32((insertedItem.FindControl("rnbTHAMNIEN") as RadNumericTextBox).Value),
                KHENTHUONG = (insertedItem.FindControl("txtKHENTHUONG") as RadTextBox).Text,
                KYLUAT = (insertedItem.FindControl("txtKYLUAT") as RadTextBox).Text,
                TRANGTHAI = (insertedItem.FindControl("rblTRANGTHAI") as RadioButtonList).SelectedValue,
                GHICHU = (insertedItem.FindControl("txtGHICHU") as RadTextBox).Text

            };
            if (rauUpload.UploadedFiles != null)
            {
                UploadedFile file = rauUpload.UploadedFiles[0];
                byte[] fileData = new byte[file.InputStream.Length];
                file.InputStream.Read(fileData, 0, (int)file.InputStream.Length);
                hocvien.PICTURE = fileData;

            }

            BVVDDataContext db = new BVVDDataContext();
            db.LST_HOCVIENs.InsertOnSubmit(hocvien);
            try
            {
                db.SubmitChanges();
                rnAlert.Text = "Thêm mới học viên thành công ";
            }
            catch (Exception ex)
            {
                rnAlert.Text = "Có lỗi xảy ra \n" + ex.Message;

            }


            rnAlert.Show();

        }

        protected void grvHocvien_UpdateCommand(object sender, GridCommandEventArgs e)
        {

            BVVDDataContext db = new BVVDDataContext();
            GridEditableItem editedItem = e.Item as GridEditableItem;
            string mahv = editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["MAHV"].ToString();

            LST_HOCVIEN hocvien = (from hv in db.LST_HOCVIENs
                                   where hv.MAHV.Equals(mahv)
                                   select hv).SingleOrDefault();
            RadAsyncUpload rauUpload = editedItem.FindControl("rauUpload") as RadAsyncUpload;



            hocvien.HODEM = (editedItem.FindControl("txtHODEM") as RadTextBox).Text;
            hocvien.TENHV = (editedItem.FindControl("txtTENHV") as RadTextBox).Text;
            hocvien.NGAYSINH = (editedItem.FindControl("rdpNGAYSINH") as RadDatePicker).SelectedDate;
            hocvien.GIOITINH = (editedItem.FindControl("ddlGIOITINH") as RadDropDownList).SelectedValue;
            hocvien.LOAIGIAY = Int32.Parse((editedItem.FindControl("rblLOAIGIAY") as RadioButtonList).SelectedValue);
            hocvien.SOCMND = (editedItem.FindControl("rntxtCMND") as RadTextBox).Text;
            hocvien.NGAYCAP = (editedItem.FindControl("rdpNGAYCAP") as RadDatePicker).SelectedDate;
            hocvien.NOICAP = (editedItem.FindControl("txtNOICAP") as RadTextBox).Text;
            hocvien.DIACHI = (editedItem.FindControl("txtDIACHI") as RadTextBox).Text;
            hocvien.HOKHAU = (editedItem.FindControl("txtHOKHAU") as RadTextBox).Text;
            hocvien.QUEQUAN = (editedItem.FindControl("txtQUEQUAN") as RadTextBox).Text;
            hocvien.TEL = (editedItem.FindControl("txtTEL") as RadTextBox).Text;
            hocvien.MOBILE = (editedItem.FindControl("txtMOBILE") as RadTextBox).Text;
            hocvien.EMAIL = (editedItem.FindControl("txtEMAIL") as RadTextBox).Text;
            hocvien.QUOCTICH = (editedItem.FindControl("ddlQUOCTICH") as RadDropDownList).SelectedValue;
            hocvien.DANTOC = (editedItem.FindControl("ddlDANTOC") as RadDropDownList).SelectedValue;
            hocvien.TONGIAO = (editedItem.FindControl("ddlTONGIAO") as RadDropDownList).SelectedValue;
            hocvien.COQUANCT = (editedItem.FindControl("txtCOQUAN") as RadTextBox).Text;
            hocvien.CHUCVU = (editedItem.FindControl("ddlCHUCVU") as RadDropDownList).SelectedValue;
            hocvien.CHUCDANH = (editedItem.FindControl("ddlCHUCDANH") as RadDropDownList).SelectedValue;
            hocvien.TRINHDO = (editedItem.FindControl("ddlTRINHDO") as RadDropDownList).SelectedValue;
            hocvien.CHUYENMON = (editedItem.FindControl("ddlCHUYENMON") as RadDropDownList).SelectedValue;
            hocvien.DANGVIEN = Int32.Parse((editedItem.FindControl("rblDANGVIEN") as RadioButtonList).SelectedValue);
            hocvien.NGAYVAODANG = (editedItem.FindControl("rdpNGAYVAODANG") as RadDatePicker).SelectedDate;
            hocvien.DOANVIEN = Int32.Parse((editedItem.FindControl("rblDOANVIEN") as RadioButtonList).SelectedValue);
            hocvien.NGAYDOANVIEN = (editedItem.FindControl("rdpNGAYDOANVIEN") as RadDatePicker).SelectedDate;
            hocvien.THUONGBINH = Int32.Parse((editedItem.FindControl("rblTHUONGBINH") as RadioButtonList).SelectedValue);
            hocvien.CONLIETSY = Int32.Parse((editedItem.FindControl("rblLIETSY") as RadioButtonList).SelectedValue);
            hocvien.THAMNIEN = Convert.ToInt32((editedItem.FindControl("rnbTHAMNIEN") as RadNumericTextBox).Value);
            hocvien.KHENTHUONG = (editedItem.FindControl("txtKHENTHUONG") as RadTextBox).Text;
            hocvien.KYLUAT = (editedItem.FindControl("txtKYLUAT") as RadTextBox).Text;
            hocvien.TRANGTHAI = (editedItem.FindControl("rblTRANGTHAI") as RadioButtonList).SelectedValue;
            hocvien.GHICHU = (editedItem.FindControl("txtGHICHU") as RadTextBox).Text;
            try
            {
                UploadedFile file = rauUpload.UploadedFiles[0];
                byte[] fileData = new byte[file.InputStream.Length];
                file.InputStream.Read(fileData, 0, (int)file.InputStream.Length);
                hocvien.PICTURE = fileData;
            }
            catch
            {

            }

            try
            {
                db.SubmitChanges();
                rnAlert.Text = "Cập nhật thành công học viên có mã số : " + mahv;
            }
            catch (Exception ex)
            {
                rnAlert.Text = "Có lỗi xảy ra. \n " + ex.Message;
            }

            rnAlert.Show();

        }
        protected void grvHocvien_DeleteCommand(object sender, GridCommandEventArgs e)
        {
            GridDataItem item = (GridDataItem)e.Item;
            string mahv = item.OwnerTableView.DataKeyValues[item.ItemIndex]["MAHV"].ToString();
            BVVDDataContext db = new BVVDDataContext();
            LST_HOCVIEN hocvien = (from hv in db.LST_HOCVIENs
                                   where hv.MAHV.Equals(mahv)
                                   select hv).SingleOrDefault();
            db.LST_HOCVIENs.DeleteOnSubmit(hocvien);
            try
            {
                db.SubmitChanges();
                rnAlert.Text = "Xóa thành công học viên có mã : " + mahv;
            }
            catch (Exception ex)
            {
                rnAlert.Text = "Có lỗi xảy ra. \n " + ex.Message;
            }

            rnAlert.Show();
        }
        protected void grvHocvien_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == RadGrid.InitInsertCommandName)
            {
                e.Canceled = true;
                System.Collections.Specialized.ListDictionary newValues = new System.Collections.Specialized.ListDictionary();
                newValues["PICTURE"] = File.ReadAllBytes(Server.MapPath("/images/no_image.jpg"));
                //e.Item.OwnerTableView.FindControl("rbiPICTURE");
                e.Item.OwnerTableView.InsertItem(newValues);
            }
        }
    }
}